package query

import (
	"math/rand"
	"time"

	"github.com/didi/nightingale/src/common/address"
	"github.com/didi/nightingale/src/toolkits/pools"
)

var (
	TransferConnPools *pools.ConnPools

	connTimeout int32
	callTimeout int32

	Config SeriesQuerySection
)

type SeriesQuerySection struct {
	MaxConn          int    `json:"maxConn"`     //
	MaxIdle          int    `json:"maxIdle"`     //
	ConnTimeout      int    `json:"connTimeout"` // 连接超时
	CallTimeout      int    `json:"callTimeout"` // 请求超时
	IndexMod         string `json:"indexMod"`
	IndexPath        string `json:"indexPath"`
	IndexCallTimeout int    `json:"indexCallTimeout"` // 请求超时
}

func Init(cfg SeriesQuerySection, hbsMod string) {
	Config = cfg
	rpcAddresses := address.GetRPCAddresses("transfer")
	cluster := make([]string, len(rpcAddresses))
	rand.Seed(time.Now().UnixNano())
	for a, b := range rand.Perm(len(rpcAddresses)) {
		cluster[a] = rpcAddresses[b]
	}
	TransferConnPools = pools.NewConnPools(Config.MaxConn, Config.MaxIdle, Config.ConnTimeout, Config.CallTimeout, cluster)

	go GetIndexLoop(hbsMod)
}
